package com.zhan.drugStore.util;

import com.zhan.drugStore.enums.ResultEnum;

import java.io.Serializable;

/**
 * 接口返回结果
 */
@SuppressWarnings("serial")
public class ResponseBase implements Serializable
{
	private static final int SUCCESS_CODE = ResultEnum.SUCCESS.getCode();
	private static final String SUCCESS_MESSAGE = ResultEnum.SUCCESS.getMessage();
	
	private int code;

	private String message;

	public int getCode()
	{
		return code;
	}

	public void setCode(int code)
	{
		this.code = code;
	}

	public ResponseBase success()
	{
		this.code = SUCCESS_CODE;
		this.message = SUCCESS_MESSAGE;
		return this;
	}
	
	public ResponseBase fail(int code, String message)
	{
		this.code = code;
		this.message = message;
		return this;
	}
	
	public ResponseBase fail(ResultEnum resultEnum)
	{
		this.setCode(resultEnum.getCode());
		this.setMessage(resultEnum.getMessage());
		return this;
	}
	
	public boolean isSuccess()
	{
		if(this.code == SUCCESS_CODE)
			return true;
		return false;
	}
	
	@Override
	public String toString()
	{
		return String.format("result->{code: [%s], message: [%s]}", this.getCode(), this.getMessage());
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}